/** 登录授权逻辑模块,不要在app.js使用 */
import {
	http
} from '@/utils/request/index.js' // 下载的插件
export default {
	redirectTo: function(pages, data) {
		let url = this.getUrl(pages, data)
		uni.redirectTo({
			url: url
		})
	},
	backTo: function(pages, data) {
		let url = this.getUrl(pages, data)
		uni.redirectTo({
			url: url
		})
	},
	Back: function(title) {
		console.log('pages Back()')

		let _timeout = 1;
		if (title) {
			uni.showToast({
				title: title,
				icon: 'success',
				duration: 2000,
			})
			_timeout = 2000
		}

		setTimeout(() => {
			let pageLength = getCurrentPages().length
			if (pageLength <= 1) {
				uni.switchTab({
					url: '/pages/tabbar/index/index'
				})
			} else {
				uni.navigateBack()
			}
		}, _timeout)



	},
	okBack: function(title) {
		let that = this
		uni.showToast({
			title: title,
			icon: 'success',
			duration: 2000,
		})
		setTimeout(() => {
			that.Back()
		}, 2000)
	},
	BackIndex: function() {
		uni.reLaunch({
			url: '/pages/index'
		})
	},
	okBackTo: function(title, pages, data) {
		let that = this
		uni.showToast({
			title: title,
			icon: 'success',
			duration: 2000,
		})
		setTimeout(() => {
			that.backTo(pages, data)
		}, 2000)
	},
	async apiTo(pages, data) {
		await http.post('/v1/index/get-link', {
			pages: pages,
			data: data
		}).then(r => {
			this.to(r.data.pages, r.data.data ? r.data.data : '')
		})
	},
	to: function(pages, data) {
		if (pages.indexOf('$') > 0) {
			return this.apiTo(pages, data)
		}

		if (pages.indexOf('://') > 0) {
			pages = '/pages/webView/webView?url=' + encodeURIComponent(pages)
			if (!data) data = []
			data.token = uni.getSkylineInfoSync('app:token')
		}

		console.log('alipayapplet', pages.indexOf('alipayapplet::'))
		if (pages.indexOf('alipayapplet::') >= 0) {
			// #ifdef MP-ALIPAY
			var _data = pages.replace('alipayapplet::', '').replace(' ', '').trim()
			console.log('alipayapplet-text', _data)
			var _partm = JSON.parse(_data)
			console.log('alipayapplet-json', _partm)
			return uni.navigateToMiniProgram({
				..._partm,
				...{
					success: function(res) {
						console.log('success', res)
					},
					fail: function(e) {
						console.log('fail', e)
					}
				}
			})
			// #endif
			// #ifndef MP-ALIPAY

			uni.showToast({
				title: '请使用支付宝打开',
				icon: 'error'
			})
			// #endif
		}




		console.log('weixinapplet', pages.indexOf('weixinapplet::'));
		if (pages.indexOf('weixinapplet::') >= 0) {
			// #ifdef MP-WEIXIN
			var _data = pages.replace('weixinapplet::', '').replace(' ', '')
			console.log('weixinapplet-text', _data, JSON.stringify(data))
			var _partm = JSON.parse(_data)
			console.log('weixinapplet', JSON.parse(_data))
			return uni.navigateToMiniProgram(_partm)
			// #endif
			// #ifndef MP-WEIXIN

			uni.showToast({
				title: '请使用微信打开',
				icon: 'error'
			})
			// #endif
		}

		if (pages.indexOf('weixinapplet::') >= 0) {
			// #ifdef MP-WEIXIN
			var _data = pages.replace('openChannelsActivity::', '').replace(' ', '')
			var _partm = JSON.parse(_data)
			console.log('openChannelsActivity', JSON.parse(_data))
			return wx.openChannelsActivity(_partm)
			// #endif
			// #ifndef MP-WEIXIN
			uni.showToast({
				title: '请使用微信打开',
				icon: 'error'
			})
			// #endif
		}


		let _tabbar = [
			'/pages/tabbar/index/index',
			'/pages/tabbar/my/my',
			'/pages/tabbar/goods/goods',
			'/pages/tabbar/school/school',
		]
		if (_tabbar.indexOf(pages) >= 0) {
			uni.switchTab({
				url: pages
			})
		} else {
			let url = this.getUrl(pages, data)
			uni.navigateTo({
				url: url
			})
		}
	},
	getUrl: function(pages, data) {
		let url = pages
		//if (typeof data == 'object' && typeof data.length > 0) {
		// data.forEach((item, key) => {
		// 	parem = parem + key + '=' +item +'&'
		// })
		//}
		if (pages.indexOf('?') <= 0) {
			url += '?'
		} else {
			url += '&'
		}
		console.log(url)
		for (let i in data) {
			let t = data[i]
			if (typeof t === 'object' || typeof t === 'array') {
				t = JSON.stringify(t)
			}
			console.log(typeof data[i])
			url += i + '=' + t + '&'
		}
		console.log(url)
		return url
	},
	isPages: function(url) {
		let pages = getCurrentPages()
		console.log(pages[pages.length - 1].route, url)
		if (pages[pages.length - 1].route == url) {
			return true
		}
		return false
	},
	getuuid() {
		let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
			return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
		})
		return uuid
	},
	error(message, complete) {
		return uni.showToast({
			title: message,
			icon: 'error',
			complete: complete ? complete : ''
		})
	},
	MPageNavigate(item) {
		console.log('MPageNavigate', item)
		if (item.extend) {
			if (item.index == 2) {
				uni.navigateTo({
					url: item.path + '?id=' + item.param.id
				});
			}
			if (item.index == 7) {
				uni.navigateTo({
					url: item.path + '?id=' + item.param.id
				});
			}
			if (item.index == 3) {
				uni.navigateTo({
					url: item.path + '?group=' + item.param.id + '&goods_show=' + item.param
						.goods_show +
						'&name=' + item.param.name
				});
			}
			// #ifndef H5
			if (item.index === 10) {
				const roomId = [item.param.roomid];
				wx.navigateTo({
					url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}`
				});
			}

			if (item.index === 11) {
				uni.navigateToMiniProgram({
					appId: item.param.applet.appId,
					path: item.param.applet.path,
					success(e) {},
					fail(e) {}
				});
			}
			// #endif
			if (item.index === 12) {
				this.to(item.param.url);
				// uni.navigateTo({
				// 	url: item.path + '?url=' + item.param.url
				// });
			}
		} else {
			if (!item || item.length === 0) return;
			if ([3, 4, 5, 1, 0].indexOf(item.index) === -1) {
				uni.navigateTo({
					url: item.path,
					fail: function() {
						uni.reLaunch({
							url: item.path
						});
					}
				});
			} else {
				uni.reLaunch({
					url: item.path,
					fail: function() {
						uni.navigateTo({
							url: item.path
						});
					}
				});
			}
		}
	}
}